package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 查询MySQL数据库，补全信息
 */
@Service
@Slf4j
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public UserInfo queryUserInfoByUserId(Long id) {
        QueryWrapper<UserInfo> query = new QueryWrapper<>();
        query.eq("user_id",id);
        UserInfo userInfo = userInfoMapper.selectOne(query);
        return userInfo;
    }

    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> queryWrapper) {
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        return userInfos;
    }

    /**
     * MySQL查询用户信息
     * @param userIds 好友动态列表的用户id
     * @return
     */
    public List<UserInfo> queryUserInfoList(Collection<?> userIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("user_id",userIds);
        return userInfoMapper.selectList(queryWrapper);
    }

    /**
     * 更新信息
     * @param userInfo
     * @return
     */
    public Boolean updateUserInfoByUserId(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        return this.userInfoMapper.update(userInfo, queryWrapper) > 0;
    }
}
